import type { ClassInfo, StudentClassResp } from './class'
import http from '@/utils/http'

// 购课项
export interface OrderItemCreateReq {
  classId: string
  lessonIndexes: number[]
}
export interface OrderItem {
  id: string
  orderId: string
  classId: string
  studentId: string
  className: string
  lessonIndexes: number[]
  actualTotalPrice: string
  actualUnitPrice: string
}
// 计算订单价格请求
export interface OrderPriceCalculateReq {
  studentId: string
  orderItemList: OrderItemCreateReq[]
  couponIdList: string[]
}
// 计算订单价格响应
export interface OrderPriceDetail {
  originalTotalPrice: number
  actualTotalPrice: number
  discountTotalPrice: number
  couponDiscountPrice: number
  packageDiscountPrice: number
  otherDiscountPrice: number
}

export interface OrderQuery {
  studentId: string
  status?: string[]
}

export interface OrderResp {
  id: string
  createTime: string
  studentId: string
  originalTotalPrice: number
  actualTotalPrice: number
  discountDetail: Record<string, string>
  payOff: boolean
  payType: string
  payTime: string
  cancelTime: string
  refunded: boolean
  remarkOut: string
  cancel: boolean
  status: string

  orderItems: OrderItem[]
}

// 下单
export interface OrderCreateReq extends OrderPriceCalculateReq {
  remarkOut?: string
  payType: string
}

// 取消订单
export interface OrderCancelReq {
  studentId: string
  orderId: string
}

// 退款订单
export interface Refund {
  id: string
  orderId: string
  enrollmentId: string
  orderItemId: string
  studentId: string
  classId: string
  cancelLessonIndexes: number[]
  refundOff: boolean
  refundPrice: number
  refundType: string
  refundTime: string
  operateSource: string
  changeType: string
}

export interface RefundResp extends Refund {
  classInfo: ClassInfo
}

export interface RefundQuery {
  studentId: string
  refundOff?: boolean
}

// 计算订单价格
export function calculatePrice(req: OrderPriceCalculateReq) {
  return http.post<OrderPriceDetail>(`/p/order/price`, req)
}

// 创建订单
export function createOrder(req: OrderCreateReq) {
  return http.post<string>(`/p/order/create`, req)
}

// 在线支付
export function onlinePay(req: { openid: string, orderId: string }) {
  return http.post(`/p/order/pay`, req)
}

// 取消订单
export const cancelOrder = (orderId: string) => {
  return http.post(`/p/order/cancel/${orderId}`)
}

// 分页查询订单
export function getOrderPage(query: OrderQuery) {
  return http.get<Page<OrderResp>>(`/p/order/page`, query)
}

// 退课订单
export function getRefundPage(query: RefundQuery) {
  return http.get<RefundResp[]>(`/p/order/refund/page`, query)
}
